<?php


require ROOT_PATH.'/Helper/admin_abstract.php';

class Action extends admin_abstract
{
    function index()
    {
        $data = _model('company')->getList('ORDER BY add_time DESC', new Pager(10));
        $this->view->assign($data);
        $this->view->display('admin/manage_company.html');
    }
    
    function delete($id)
    {
        $id = intval($id);
        if ($id) {
            _model('company')->delete(array('id'=>$id));
        }
        $this->redirect();
    }

    function delete_email($id)
    {
        $id = intval($id);
        if ($id) {
            _model('company_email')->delete(array('id'=>$id));
        }
        $this->redirect();
    }

    function company_email($id)
    {
        $id = intval($id);
        if (!$info = _model('company')->read(array('id'=>$id))) {
            $this->msg('Operation Failed, bad request, Back to continue');
        }
        if (Request::isPost()) {
            $email = _POST('email');
            if (empty($email)) {
                $this->msg('Operation Failed, bad request, Back to continue');
            }
            if (_model('company_email')->read(array('email'=>$email))) {
                $this->msg('email adress exists');
            }
            _model('company_email')->create(array(
                'company_id' => $id,
                'email' => $email,
                'add_time' => time()
            ));
            $this->redirect("admin/company_manage/company_email/$id.html");
        } else {
            $data = _model('company_email')->getList(array('company_id'=>$id), 'ORDER BY add_time DESC');
            $this->view->assign('data', $data);
            $this->view->assign('info', $info);
            $this->view->display('admin/company_email.html');
        }
    }

    function add()
    {
        if ($id = _GET('id', 0)) {
            $info = _model('company')->read(array('id'=>$id));
            if (empty($info)) {
                $this->msg('Operation Failed, bad request, Back to continue');
            }
        }

        
        if (Request::isPost()) {
            $company_name = _POST('company_name');

            if (!$id || $info['company_name'] != $company_name) {
                if (_model('company')->read(array('company_name'=>$company_name))) {
                    $this->msg('company exists');
                }
            }

            if ($id) {
                if ($info['company_name'] != $company_name) {
                    _model('company')->update(array('id'=>$id), array('company_name'=>$company_name));
                }
            } else {
                _model('company')->create(array(
                    'company_name' => $company_name,
                    'add_time' => time(),
                    'user_id' => $this->info['id']
                ));
            }
            $this->redirect('admin/company_manage.html');
        } else {
            if ($id) {
                $this->view->assign('info', $info);
            }
            $this->view->display('admin/add_company.html');
        }
    }
}


?>